// mycomponent/voice_shop/good_detail.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    goodsId: {
      type: null,
    },
    enterAt: {
      type: null,
    },
    leaveAt: {
      type: null,
    },
    hoet: {
      type: null,
    },
    mark: {
      type: null,
    },
    markId: {
      type: null,
    },
  },

  /**
   * 组件的初始数据
   */
  data: {
    stop1: `${app.globalData.imgPath}stop1.png`,
    play1: `${app.globalData.imgPath}play1.png`,
    phone1: `${app.globalData.imgPath}phone1.png`,
    weixin1: `${app.globalData.imgPath}weixin.png`,
    bfstatus: false,
    posterShow: false,
    innerAudioContext: null,
  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {
      this.getData();
      this.init();
      console.log(this.data.goodsId);
    },
    moved() {},
    detached() {
      this.data.innerAudioContext.destroy();
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    onlinekf(e) {
      var that = this;
      console.log(e);
      wx.setClipboardData({
        data: that.data.people_connect,
        success(res) {
          wx.getClipboardData({
            success(res) {
              wx.showToast({
                title: "已复制链接请用浏览器打开",
                icon: "none",
              });
            },
          });
        },
      });
    },
    phone(e) {
      console.log(this.data.ConnectionInfo.phone);
      var phoneNum = this.data.ConnectionInfo.phone;
      wx.makePhoneCall({
        phoneNumber: phoneNum, //仅为示例，并非真实的电话号码
      });
    },
    posterClose(e) {
      this.setData({
        posterShow: false,
      });
    },
    popupSpecs(e) {
      this.getMessage();
      this.setData({
        posterShow: true,
      });
    },
    tapzt(e) {
      this.data.innerAudioContext.pause();
      this.setData({
        bfstatus: !this.data.bfstatus,
      });
    },
    gotoGoods(e) {
      console.log(e.currentTarget.id);
      wx.navigateTo({
        url: "/packageA/detail_v2/detail_v2?id=" + e.currentTarget.id,
      });
    },
    tapbf(e) {
      this.setData({
        bfstatus: !this.data.bfstatus,
      });
      console.log(e.currentTarget.dataset.dali.url);
      var musiUrl = e.currentTarget.dataset.dali.url;
      this.data.innerAudioContext.src = musiUrl;
      this.data.innerAudioContext.play();
    },
    gotoindex() {
      wx.reLaunch({
        url: "/packageG/index/index",
      });
    },
    gotoMember() {
      wx.reLaunch({
        url: "/packageG/member_v2/member_v2",
      });
    },
    getMessage() {
      let that = this;
      let urlStr = app.getNetAddresss("plugin.voice-goods.api.voice-goods.getConnection");
      app._postNetWork({
        url: urlStr,
        success: function (resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            that.setData({
              ConnectionInfo: res.data,
              people_connect: res.data.people_connect,
            });
          }
        },
        fail: function (res) {},
      });
    },
    getData() {
      console.log(this.data.goodsId);
      let that = this;
      let urlStr = app.getNetAddresss("plugin.voice-goods.api.voice-goods.getVoiceGoods");
      app._postNetWork({
        url: urlStr,
        data: {
          goods_id: that.data.goodsId,
        },
        success: function (resdata) {
          that.setData({
            goodsInfo: resdata.data.data,
            is_rule: resdata.data.data.is_rule == 1 ? true : false,
          });
          wx.setNavigationBarTitle({
            title: resdata.data.data.title,
          });
        },
        fail: function (res) {},
      });
    },
    init() {
      var that = this;
      this.data.innerAudioContext = wx.createInnerAudioContext();
      this.data.innerAudioContext.onCanplay(() => {});
      this.data.innerAudioContext.onPlay(() => {
        console.log("开始播放");
        console.log(this.data.innerAudioContext.duration);
        this.setData({
          currentTime: this.realFormatSecond(this.data.innerAudioContext.duration),
          slmin: "0:00:00",
        });
      });
      this.data.innerAudioContext.onTimeUpdate(() => {
        setTimeout(() => {
          let smun = (this.data.innerAudioContext.currentTime / this.data.innerAudioContext.duration) * 100;
          var valueSlmin = parseInt(smun);
          console.log(this.data.innerAudioContext.duration);
          that.setData({
            currentTime: this.realFormatSecond(this.data.innerAudioContext.duration),
            slmin: this.realFormatSecond(this.data.innerAudioContext.currentTime),
            valueSlmin: valueSlmin,
            audio_length: this.data.innerAudioContext.duration,
          });
        }, 1000);
      });
      this.data.innerAudioContext.onError((res) => {
        wx.showToast({
          title: "播放错误",
          icon: "none",
        });
      });
    },
    sildein(e) {
      console.log(e);
      var setvalue = e.detail.value / 100;
      var long = this.data.audio_length * setvalue;
      console.log(long, setvalue);
      this.data.innerAudioContext.seek(long);
    },
    realFormatSecond(second) {
      var secondType = typeof second;

      if (secondType === "number" || secondType === "string") {
        second = parseInt(second);

        var hours = Math.floor(second / 3600);
        second = second - hours * 3600;
        var mimute = Math.floor(second / 60);
        second = second - mimute * 60;

        return hours + ":" + ("0" + mimute).slice(-2) + ":" + ("0" + second).slice(-2);
      } else {
        return "0:00:00";
      }
    },
  },
});
